﻿using ASoft.Msg;
using ASoft.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ASoft.WPF.Base
{
    public class RoomTask
    {
        public RealTimeItem GetRealTimeItem()
        {
            return new RealTimeItem(
                DateTime.Now.AddSeconds(10),
                () =>
                {
                    try
                    {
                        WSSearch();
                        RoomSearch();
                    }
                    finally
                    {
                        MyTaskService.Current.AddItem(GetRealTimeItem());
                    }

                },
                "Room Task");
        }

        void WSSearch()
        {
            if (RC.Current.RootData.启用模似气象站)
            {
                return;
            }

            var sendMsg = new WeatherStationSearchMsg();
            var returnMsg = RC.Current.MsgService.SendMsg(sendMsg);

            if (returnMsg == null && RC.Current.RootData.气象站实时信息 == null)
            {
                throw new Exception("获得气象站数据出错.");
            }

        }

        void RoomSearch()
        {

            foreach (var room in RC.Current.RootData.温室集合)
            {
                DoPhoneMsgSet();
                if (RC.Current.RootData.IgnoreRoomIds.Contains(room.Id))
                {
                    Thread.Sleep(10);
                    continue;
                }
                if (room.IsPhec)
                {
                    SendPhecSearch(room);
                }
                else
                {
                    if (RC.Current.RootData.气象站实时信息 == null)
                    {
                        Thread.Sleep(10);
                        continue;
                    }
                    SendRoomSearchMsg(room);
                }
                (new HttpPostService()).Post();
            }
        }

        void DoPhoneMsgSet()
        {
            var list = RC.Current.PhoneMsgs.ToList();
            RC.Current.PhoneMsgs.Clear();
            list.ForEach(pm =>
            {
                if (!PhoneMsgService.PhoneMsgCanUse(pm))
                {
                    RC.Current.Loger.Trace(string.Format("Phone:{0},不被允许进行控制", pm.PhoneNumber));
                    return;
                }
                string returnMsg;
                var roomSet = PhoneMsgService.GetAndChangeRoomSet(pm, out returnMsg);
                if (!string.IsNullOrWhiteSpace(returnMsg))
                {
                    RC.Current.Loger.Trace(string.Format("手机消息不匹配，提示信息为:{0}", returnMsg));
                    RC.Current.Loger.Trace(string.Format("手机消息内容为:{0}", pm.ToString()));
                }
                RC.Current.SaveRootData();
                //如果无线模块没有返回消息，就再发送
                for (int i = 0; i < 5; i++)
                {
                    Thread.Sleep(2000);
                    var returnSet = RC.Current.MsgService.TransSendMsg(roomSet);
                    if (returnSet != null)
                    {
                        break;
                    }
                }

                Thread.Sleep(2000);

            });
        }

        private void SendPhecSearch(RoomData room)
        {
            var send = new PhecSearch()
            {
                Id = room.Id
            };
            var msg = RC.Current.MsgService.SendMsg(send);
        }

        private void SendRoomSearchMsg(RoomData room)
        {
            var date = DateTime.Now;
            var roomSearchMsg = new RoomSearchMsg()
            {
                Id = room.Id,
                风速 = RC.Current.RootData.气象站实时信息.风速,
                风向 = RC.Current.RootData.气象站实时信息.风向,
                光照强度 = RC.Current.RootData.气象站实时信息.光照,
                上位机时间小时 = (byte)date.Hour,
                上位机时间分钟 = (byte)date.Minute,
                上位机时间秒钟 = (byte)date.Second,
                室外温度 = RC.Current.RootData.气象站实时信息.室外温度,
                雨雪信号 = RC.Current.RootData.气象站实时信息.雨雪

            };
            var msg = RC.Current.MsgService.SendMsg(roomSearchMsg);
        }
    }
}
